package dao;

import dbc.DBI;
import dbc.MyConn;
import vo.News;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewDaoImpl implements INewDao {
    public static void main(String[] args) {
        try {
            System.out.println( new NewDaoImpl().gatAllNew().size());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @Override
    public List<News> gatAllNew() throws SQLException {
        List<News> list=new ArrayList<>();
        //打开数据库
        DBI dbi =new MyConn();
        Connection connection = dbi.openConn();
        //发送sql语句
        PreparedStatement ps = connection.prepareStatement("select * from naws ");
        ResultSet rs = ps.executeQuery();//所有的记录已经放入虚拟表中了 // ps.executeQuery()
        while (rs.next()){
            News news = new News();
            news.setId(rs.getInt(1));
            news.setAuther(rs.getString(4));
            news.setContent(rs.getString(3));
            news.setTitle(rs.getString(2));
            news.setTime(rs.getString(5));

            list.add(news);
        }
        //释放资源  关闭数据库
        dbi.closeAll(null,ps,rs);
        dbi.closeConn();

        return list;
    }

    @Override
    public News getOneNewByID(int id) throws SQLException {
        return null;
    }

    @Override
    public boolean deleteByid(int id) throws SQLException {
        return false;
    }
}
